﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AES小工具
{
    public partial class FrmIotDecode : Form
    {
        public FrmIotDecode()
        {
            InitializeComponent();
        }

        private void btnDecode_Click(object sender, EventArgs e)
        {
            string cipherText = tbCiphertext.Text.Trim(); //Regex.Replace( tbCiphertext.Text.Trim(), "[A-Za-z0-9]", "",RegexOptions.IgnoreCase);
            
            byte[] bytes = null;
            if (cipherText.Contains(" "))
            {
                //cipherText = Regex.Replace(cipherText, " ", "", RegexOptions.IgnoreCase);
                //cipher = cipherText.Split(' ');
                string[] strs = cipherText.Split(' ');
                bytes = new byte[strs.Length];  
                for(int i=0;i<strs.Length;i++) {
                    bytes[i] = Convert.ToByte(strs[i], 16);
                }
            }
            else {
                char[] cipher = cipherText.ToCharArray();
                //char[] cipher = cipherText.ToCharArray();
                bytes = new byte[cipher.Length / 2];

                for (int i = 0; i < bytes.Length; i++)
                {
                    string str = cipher[i * 2].ToString();
                    str += cipher[(i * 2) + 1].ToString();
                    bytes[i] = Convert.ToByte(str, 16);
                }
            }
            

            byte[] buids = new byte[20];
            Array.Copy(bytes,1,buids,0,20);

            string passowrd = getPassword(buids);

            int mpStartIndex = 1 + 20 + 2 + 1;
            int mpLen = bytes.Length - mpStartIndex - 4 - 1;

            byte[] mpBody = new byte[mpLen];
            Array.Copy(bytes,mpStartIndex,mpBody,0,mpLen);

            string debugText = "";
            for (int i = 0; i < mpBody.Length; i++) {
                if (!debugText.Equals(""))
                    debugText += " ";
                debugText += mpBody[i].ToString("X2");
            }

            /*Console.WriteLine("<passowrd:{0}>", passowrd);
            Console.WriteLine(debugText);

            return;*/
            //解密
            byte[] deCodeBytes = AESHelper.AESDecrypt(mpBody, passowrd);
            string text = "密码:" + passowrd + "\r\n";
            /*for(int i = 0;i < deCodeBytes.Length;i++)
            {
                if (!text.Equals(""))
                    text += " ";
                text += deCodeBytes[i].ToString("X2");
            }*/
            bool isEnd = false;
            int startIndex = 0;
            do {
                if (deCodeBytes[startIndex] == 0) { 
                    isEnd = true;
                    break;
                }
                text += deCodeBytes[startIndex++].ToString("X2") + " ";
                int len = deCodeBytes[startIndex];
                len = (len << 8) + deCodeBytes[startIndex + 1];
                text += deCodeBytes[startIndex++].ToString("X2") + " ";
                text += deCodeBytes[startIndex++].ToString("X2") + " ";
                for (int i = 0; i < len; i++) {
                    text += deCodeBytes[startIndex++].ToString("X2") + " ";
                }
                text += deCodeBytes[startIndex++].ToString("X2") + "\r\n";
            } while (!isEnd);
            tbText.Text = text;

        }

        private string getPassword(byte[] buids) {
            
            string password = "";

            int[] inds = new int[] { 1, 2, 3, 4, 11, 12, 13, 14, 6, 7, 8, 9, 16, 17, 18, 19 };
            byte[] passBytes = new byte[inds.Length];
            for(int i = 0; i < inds.Length; i++) {
                passBytes[i] = buids[inds[i]];
            }

            password = Encoding.ASCII.GetString(passBytes);

            return password;
        }
    }
}
